package com.wxlsoft.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.swing.JOptionPane;

public class ShowErrorMessage {

	public static void show(String message) {
		JOptionPane.showMessageDialog(null, message,  "错误" ,0);
	}

	public static void error(Throwable throwable) {
		JOptionPane.showMessageDialog(null, buildstack(throwable),  "错误" ,0);
	}	
	
	/**
	 * 执行成功弹窗
	 */
	public static void successMessage() {
		JOptionPane.showMessageDialog(null, "success!", "提示", JOptionPane.PLAIN_MESSAGE);
	}
	
	/**
	 * 输出堆栈信息
	 * @param throwable
	 * @return
	 */
	private static Object buildstack(Throwable throwable) {
		StackTraceElement[] stackTrace = throwable.getStackTrace();
		if (Objects.nonNull(stackTrace)) {
			List<String> stackInfo = new ArrayList<>();
			String message = throwable.getMessage();
			stackInfo.add(String.format("%s :%s \r\n", throwable.getClass().toString(), Objects.nonNull(message) ? message : ""));
			for (StackTraceElement stack : stackTrace) {
				stackInfo.add(String.format("%s \r\n", stack.toString()));
			}
			return stackInfo;
		}
		return "未知错误";
	}	
	
	
}
